﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;

namespace CNVP.JShop.Web.Admin
{
    public partial class UserList : AdminPage
    {
        BLL.CNVP_Users bll = new BLL.CNVP_Users();
        public int rplistCount = 0;
        public int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 89)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                ViewState["SearchType"] = "0";
                UserLevelBind();
                StartLoad(PageIndex);
                btDelete.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定删除该记录吗?');}else{alert('请选择记录！');return false;};");
            }
        }

        private void UserLevelBind()
        {
            IList<Model.CNVP_UserLevel> model_UserLevel = new BLL.CNVP_UserLevel().GetAll_CNVP_UserLevel();
            foreach (Model.CNVP_UserLevel m in model_UserLevel)
            {
                ddlUserLevel.Items.Add(new ListItem(m.LevelName, m.ID.ToString()));
            }
        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            SQLConditionInfo[] st = new SQLConditionInfo[4];
            st[0] = new SQLConditionInfo("@UserLevelID", ddlUserLevel.SelectedValue);
            st[1] = new SQLConditionInfo("@NewsTitle", Public.FilterSql(txtkey.Text.Trim()));
            st[2] = new SQLConditionInfo("@NewsTitle1", Public.FilterSql(txtkey1.Text.Trim()));
            st[3] = new SQLConditionInfo("@SearchType", ViewState["SearchType"]);
            int i, j;
            DataTable dt = null;
            int pageNum = 20;
            dt = bll.GetPage(this.GetType().Name, PageIndex, pageNum, out i, out j, st);
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //方法一
            if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
            {
                LinkButton LinkBTStartPwd = (LinkButton)e.Item.FindControl("LinkBTStartPwd");
                LinkBTStartPwd.Attributes.Add("onclick", "return confirm('确定初始化密码吗?');");
            }
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            if (e.CommandName == "PointsUDRcord")
            {
                Response.Redirect("PointsUDRcordList.aspx?UserID=" + id);
            }
            if (e.CommandName == "StartPwd")
            {
                new BLL.CNVP_Users().UpdatePwd(EncryptUtils.MD5("123456"), new BLL.CNVP_Users().GetSingle_CNVP_UsersById(id).UserName);
                BasePage.MessageBoxGoHistory("初始化成功！");
            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    int code = int.Parse(cbCheck.ToolTip);
                    var OrderUser_List = new BLL.CNVP_Order().GetAll_CNVP_Order("BuyerID=" + code);
                    if (OrderUser_List.Count > 0)
                    {
                        BasePage.MessageBox("" + new BLL.CNVP_Users().GetSingle_CNVP_UsersById(code).UserName + "-该产品存在订单不允许删除！");
                    }
                    else
                    {
                        bll.Delete_CNVP_Users(code);
                    }
                }
            }
            BasePage.MessageBox("删除操作成功！", "UserList.aspx");
        }

        protected void btHighSearch_Click(object sender, EventArgs e)
        {
            ViewState["SearchType"] = "1";
            StartLoad(PageIndex);

        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserEdit.aspx");
        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["SearchType"] = "0";
            StartLoad(PageIndex);
        }
    }
}